<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发布人" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入发布人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['center:assignment:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assignmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="发布人" align="center" prop="nickName"/>
      <el-table-column label="手机号" align="center" prop="phonenumber"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span> {{ scope.row.sex === 0 ? '男' : scope.row.sex === 1 ? '女' : '未知' }} </span>
        </template>
      </el-table-column>
      <el-table-column label="部门" align="center" prop="deptName"/>
      <el-table-column label="岗位" align="center" prop="postName"/>
      <el-table-column label="任务标题" align="center" prop="title"/>
      <el-table-column label="任务描述" align="center" prop="description"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220px">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-finished"
            size="mini"
            v-hasPermi="['center:assignment:take']"
            @click="handleTake(scope.row)"
          >接下任务
          </el-button>
          <el-button
            type="text"
            icon="el-icon-folder"
            size="mini"
            @click="fileVisible(scope.row)"
          >预览文件
          </el-button>
          <el-button
            type="text"
            icon="el-icon-download"
            size="mini"
            v-hasPermi="['center:assignment:download']"
            @click="handleDownload(scope.row)"
          >下载文件
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="预览文件" width="60%" :visible.sync="dialogVisible" center>
        <vue-office-docx
          :src="fileUrl"
          v-if="fileType === 'docx'"
        />
        <vue-office-pdf
          style="height: 75vh"
          :src="fileUrl"
          v-else-if="fileType === 'pdf'"
        />
      <VueOfficeExcel
        style="height: 75vh"
        :src="fileUrl"
        v-else-if="fileType === 'xlsx'"
      />
      <vue-office-pptx
      :src="fileUrl"
      v-else-if="fileType === 'pptx'"
      />
      <div v-else>
        只预览支持 .docx .pdf .excel .pptx 文件的预览
      </div>

    </el-dialog>

  </div>
</template>

<script>
import {listAssignment, tackAssignment} from "@/api/center/assignment"
import {downloadFileToMinio} from "@/utils/fileutils/fileUtils";

//引入VueOfficeDocx组件
import VueOfficeDocx from '@vue-office/docx'
import VueOfficePdf from "@vue-office/pdf";
import VueOfficePptx from "@vue-office/pptx";
import VueOfficeExcel from "@vue-office/excel";
//引入相关样式
import '@vue-office/docx/lib/index.css'
import '@vue-office/excel/lib/index.css'

export default {
  name: "Assignment",
  components:{
    VueOfficeDocx,
    VueOfficePdf,
    VueOfficePptx,
    VueOfficeExcel
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务中心表格数据
      assignmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 更新时间时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 文件路径
      fileUrl: '',
      fileType:'',
      // 对话框
      dialogVisible: false,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询任务中心列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0]
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1]
      }
      listAssignment(this.queryParams).then(response => {
        this.assignmentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        description: null,
        publisherId: null,
        recipientId: null,
        accomplishTime: null,
        status: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('center/assignment/export', {
        ...this.queryParams
      }, `assignment_${new Date().getTime()}.xlsx`)
    },
    /** 接下任务*/
    handleTake(row) {
      const fileId = row.fileId
      const id = row.id
      this.$modal.confirm('是否确认要接下为' + row.title + '的任务').then(function () {
      }).then(() => {
        tackAssignment(id, fileId).then(() => {
          this.$modal.msgSuccess('接下任务成功')
          this.handleQuery()
        })
      }).catch(() => {
      })
    },
    handleDownload(row) {
      const fileName = row.fileName;
      const originalName = row.originalName || fileName; // 使用原文件名更友好
      downloadFileToMinio(fileName, originalName)
    },
    /** 预览文件*/
    fileVisible(row) {
      this.dialogVisible = true
      let lastIndex = row.url.lastIndexOf('.');
      this.fileType = lastIndex === -1 ? '' : row.url.substring(lastIndex + 1);
      this.fileUrl = row.url
    }
  }
}
</script>
